package com.xinqi.modules.chat.sysmessage.service.impl;

import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.core.constant.CacheConstants;
import com.xinqi.common.core.utils.StringUtils;
import com.xinqi.common.redis.utils.RedisUtils;
import com.xinqi.modules.chat.sysmessage.config.BaseConfig;
import com.xinqi.modules.chat.sysmessage.domain.SysRongYunUserEntity;
import com.xinqi.modules.chat.sysmessage.manager.SysRongYunUserManager;
import com.xinqi.modules.chat.sysmessage.service.SysRongYunUserService;
import com.xinqi.modules.chat.third.rongyun.service.RongyunService;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 系统融云用户发消息业务层
 * @author: dzy
 * @date: 2023/03/23
 */
@Service
@RequiredArgsConstructor
public class SysRongYunUserServiceImpl implements SysRongYunUserService {

    private final SysRongYunUserManager sysRongYunUserManager;

    private final RongyunService rongyunService;

    @Override
    public boolean save() {
        SysRongYunUserEntity maxEntity = sysRongYunUserManager.getMaxId();
        Long id = BaseConfig.baseId;
        if (Objects.nonNull(maxEntity)) {
            id = maxEntity.getId();
        }
        SysRongYunUserEntity entity = new SysRongYunUserEntity();
        entity.setId(id + 1);
        entity.setNickname(BaseConfig.baseName);
        entity.setRongyunToken(rongyunService.registUser(entity.getId().toString(), entity.getNickname(), ""));
        boolean flag = sysRongYunUserManager.insert(entity);
        this.refreshRedis();
        return flag;
    }

    @Override
    public boolean saveBatch(int number) {
        CommonErrors.BAD_REQUEST.check(number <= 20, "创建数量一次不可已超过200个");
        SysRongYunUserEntity maxEntity = sysRongYunUserManager.getMaxId();
        Long id = BaseConfig.baseId;
        if (Objects.nonNull(maxEntity)) {
            id = maxEntity.getId();
        }
        List<SysRongYunUserEntity> list = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            SysRongYunUserEntity entity = new SysRongYunUserEntity();
            entity.setId(id + i + 1);
            entity.setNickname(BaseConfig.baseName);
            entity.setRongyunToken(rongyunService.registUser(entity.getId().toString(), entity.getNickname(), ""));
            if (StringUtils.isNotEmpty(entity.getRongyunToken())) {
                list.add(entity);
            }
        }
        boolean flag = sysRongYunUserManager.insertBatch(list);
        this.refreshRedis();
        return flag;
    }

    @Override
    public void refreshRedis() {
        List<SysRongYunUserEntity> list = this.sysRongYunUserManager.list();
        // 读取一个临时token将剩余token更新到缓存中
        RedissonClient redisson = RedisUtils.getClient();
        RQueue<SysRongYunUserEntity> queue = redisson.getQueue(CacheConstants.SYS_RONGYUN_KEY_LIST);
        queue.clear();
        list.forEach(item -> {
            queue.add(item);
        });
    }
}
